package com.hqj.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * java的日历
 */
public class Test03 {

    public static void main(String[] args) {
        CalendarClass cc = new CalendarClass();
        cc.method3();
    }
}
class CalendarClass{

    public void method(){
        //创建日历对象
        Calendar c = Calendar.getInstance();
        int y = c.get(Calendar.YEAR);//年
        int m = c.get(Calendar.MONTH)+1;//月  从0开始  0-1 1-2
        int d = c.get(Calendar.DAY_OF_MONTH);//日

        int h = c.get(Calendar.HOUR);//小时  HOUR 12小时制  HOUR_OF_DAY  24小时制
        int mi = c.get(Calendar.MINUTE);//分钟
        int s = c.get(Calendar.SECOND);//秒
        int ms = c.get(Calendar.MILLISECOND);//毫秒

        int w = c.get(Calendar.DAY_OF_WEEK);//星期的第几天  星期天是星期的第一天

        System.out.println(y+"年"+m+"月"+d+"日 "+h+"时"+mi+"分"+s+"秒，星期"+(w-1));

    }

    public void method2(){
        //创建日历对象
        Calendar c = Calendar.getInstance();
        //日历转换为时间Date
        Date d = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
        System.out.println(sdf.format(d));

        //时间Date转换为日历
        c.setTime(d);

        //日历转换为long
        long l1 = c.getTimeInMillis();
        //long 转换为 日历
        c.setTimeInMillis(l1);
    }

    public void method3(){
        //创建日历对象
        Calendar c = Calendar.getInstance();

        //180天后是星期几？
        c.add(Calendar.DAY_OF_MONTH,-180);//正数相加，负数相减
        int w = c.get(Calendar.DAY_OF_WEEK);
        String ws[] = {"天","一","二","三","四","五","六"};
        System.out.println("星期"+ws[w-1]);

        //时间的设置
        c.set(Calendar.YEAR,1999);
        //注意月份是从0开始的
        c.set(2021,8,1);

        Calendar c1 = Calendar.getInstance();
        int w1 = c1.get(Calendar.DAY_OF_WEEK);
        System.out.println("设置前星期"+w1);
        //TODO false
        c1.setFirstDayOfWeek(Calendar.MONDAY);
        w1 = c1.get(Calendar.DAY_OF_WEEK);
        System.out.println("设置后星期"+w1);
    }


}
